{% extends "admin/base.html" %}

{% block title %}仪表盘{% endblock %}

{% block page_title %}仪表盘{% endblock %}

{% block extra_css %}
<style>
/* 统计卡片样式 */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: #fff;
    padding: 1.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 1rem;
}

.stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: white;
}

.stat-card:nth-child(1) .stat-icon {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-card:nth-child(2) .stat-icon {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-card:nth-child(3) .stat-icon {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-card:nth-child(4) .stat-icon {
    background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-card:nth-child(5) .stat-icon {
    background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.stat-card:nth-child(6) .stat-icon {
    background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
}

.stat-content h3 {
    font-size: 2rem;
    color: #2c3e50;
    margin-bottom: 0.25rem;
    font-weight: 700;
}

.stat-content p {
    color: #7f8c8d;
    margin: 0;
    font-size: 0.9rem;
    font-weight: 500;
}

/* 图表区域样式 */
.charts-section {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.chart-container {
    background: #fff;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.chart-container h2 {
    margin-bottom: 1.5rem;
    color: #2c3e50;
    font-size: 1.3rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.chart-container h2:before {
    content: '';
    width: 4px;
    height: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 2px;
}

.chart-placeholder {
    height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f9fa;
    border-radius: 8px;
    border: 2px dashed #dee2e6;
}

/* 数据表格区域 */
.data-section {
    background: #fff;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    margin-bottom: 2rem;
}

.data-table h2 {
    margin-bottom: 1.5rem;
    color: #2c3e50;
    font-size: 1.3rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.data-table h2:before {
    content: '';
    width: 4px;
    height: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 2px;
}

.data-table .table {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.data-table .table th {
    background: transparent;
    color: #333;
    font-weight: 600;
    border: none;
    padding: 1rem;
    border-bottom: 2px solid #dee2e6;
}

.data-table .table td {
    padding: 1rem;
    border-bottom: 1px solid #f1f3f4;
    vertical-align: middle;
}


.data-table .table a {
    color: #007bff;
    text-decoration: none;
    font-weight: 500;
}

.data-table .table a:hover {
    text-decoration: underline;
    color: #0056b3;
}
</style>
{% endblock %}

{% block content %}
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-file-alt"></i>
            </div>
            <div class="stat-content">
                <h3>{{ stats.total_posts }}</h3>
                <p>总文章数</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-eye"></i>
            </div>
            <div class="stat-content">
                <h3>{{ stats.total_views }}</h3>
                <p>总访客量</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-thumbs-up"></i>
            </div>
            <div class="stat-content">
                <h3>{{ stats.total_likes }}</h3>
                <p>总点赞量</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-comments"></i>
            </div>
            <div class="stat-content">
                <h3>{{ stats.total_comments }}</h3>
                <p>总评论数</p>
            </div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-chart-bar"></i>
            </div>
            <div class="stat-content">
                <h3>{{ stats.monthly_views }}</h3>
                <p>本月访客</p>
            </div>
        </div>
    </div>
    
    <!-- 图表区域 -->
    <div class="charts-section">
        <div class="chart-container">
            <h2>访客趋势</h2>
            <div class="chart-placeholder">
                <canvas id="visitorChart" width="400" height="200"></canvas>
            </div>
        </div>
        
        <div class="chart-container">
            <h2>文章分类分布</h2>
            <div class="chart-placeholder">
                <canvas id="categoryChart" width="400" height="200"></canvas>
            </div>
        </div>
    </div>
    
    <!-- 数据表格 -->
    <div class="data-section">
        <div class="data-table">
            <h2>热门文章</h2>
            <table class="table">
                <thead>
                    <tr>
                        <th>文章标题</th>
                        <th>浏览量</th>
                        <th>点赞数</th>
                        <th>评论数</th>
                        <th>发布时间</th>
                    </tr>
                </thead>
                <tbody>
                    {% for post in popular_posts %}
                    <tr>
                        <td><a href="{{ post.html_path or '/static/html/' + (post.published_at.strftime('%Y/%m/%d') if post.published_at else post.created_at.strftime('%Y/%m/%d')) + '/' + (post.published_at.strftime('%Y%m%d') if post.published_at else post.created_at.strftime('%Y%m%d')) + '_' + str(post.id)[:8] + '.html' }}">{{ post.title }}</a></td>
                        <td>{{ post.view_count }}</td>
                        <td>{{ post.like_count or 0 }}</td>
                        <td>{{ post.comment_count or 0 }}</td>
                        <td>{{ post.created_at.strftime('%Y-%m-%d') }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}

{% block extra_js %}
<script>
// 访客趋势图表
const visitorCtx = document.getElementById('visitorChart').getContext('2d');
const visitorChart = new Chart(visitorCtx, {
    type: 'line',
    data: {
        labels: {{ stats.visitor_trend.labels | tojson }},
        datasets: [{
            label: '访客量',
            data: {{ stats.visitor_trend.data | tojson }},
            borderColor: 'rgb(75, 192, 192)',
            backgroundColor: 'rgba(75, 192, 192, 0.2)',
            tension: 0.1
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                display: false
            }
        },
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});

// 文章分类分布图表
const categoryCtx = document.getElementById('categoryChart').getContext('2d');
const categoryChart = new Chart(categoryCtx, {
    type: 'doughnut',
    data: {
        labels: {{ stats.category_distribution.labels | tojson }},
        datasets: [{
            data: {{ stats.category_distribution.data | tojson }},
            backgroundColor: [
                'rgba(255, 99, 132, 0.8)',
                'rgba(54, 162, 235, 0.8)',
                'rgba(255, 205, 86, 0.8)',
                'rgba(75, 192, 192, 0.8)',
                'rgba(153, 102, 255, 0.8)',
                'rgba(255, 159, 64, 0.8)',
                'rgba(199, 199, 199, 0.8)',
                'rgba(83, 102, 255, 0.8)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 205, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)',
                'rgba(199, 199, 199, 1)',
                'rgba(83, 102, 255, 1)'
            ],
            borderWidth: 2
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'bottom'
            }
        }
    }
});
</script>
{% endblock %}